import { login, sysProfile, sysUser } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: ''
}
const mutations = {
  setToken (state, value) {
    state.token = value
    setToken(value)
  },
  setUserInfo (state, value) {
    state.setUserInfo = value
  },
  // 退出登录处理
  logout (state) {
    state.token = ''
    setToken('')
    state.userInfo = ''
  }
}
const actions = {
  // 登陆调用
  async toLogin ({ commit }, value) {
    // login(value)
    //   .then((res) => {
    //     console.log('res', res)
    //     commit('setToken', res.data)
    //   })
    //   .catch((err) => {
    //     console.log('err', err)
    //   })
    const res = await login(value)

    commit('setToken', res.data)
    console.log(res)
  },
  // 获取个人用户信息
  async getUserInfo ({ commit }) {
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    commit('setUserInfo', { ...res.data, ...res2.data })

    console.log('个人用户信息', res, res2)
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
